package com.ks.repair.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import com.ks.repair.entity.BRepairFees;
import com.ks.repair.dao.BRepairFeesMapper;
import com.ks.repair.service.IBRepairFeesService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.repair.service._ServiceBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报修-租户维护的服务项目的标准收费说明清单 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-16
 */
@Service
public class BRepairFeesServiceImpl extends _ServiceBase<BRepairFeesMapper, BRepairFees> implements IBRepairFeesService {
    @Resource
    private  BRepairFeesMapper bRepairFeesMapper;

    @Override
    public Map<String, Object> selectRepairFees(OperationContext context,SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BRepairFees> wrapper =new EntityWrapper<>();
        BRepairFees bRepairFees  =new BRepairFees();
        if(ToolUtil.isNotEmpty(fields)){
            for(SearchCriteriaBase.matchField field:fields){
                if("id".equals(field.getName())){
                    bRepairFees.setTenantID(Integer.getInteger(field.getValue()));
                }
            }
        }
        if(!isSystem(context)){
            bRepairFees.setTenantID(context.getTenantId().intValue());
        }
        bRepairFees.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(bRepairFees);
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BRepairFees> bRepairFees1s = bRepairFeesMapper.selectPage(limit,wrapper);
        Map<String, Object>  resultMap = this.isResultMap(bRepairFees1s,searchCriteriaBase);
        Integer count = bRepairFeesMapper.selectCount(wrapper);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int addRepairFees(OperationContext context,BRepairFees bRepairFees) {
        if(!isSystem(context)){
            bRepairFees.setOrgID(context.getOrgId().intValue());
            bRepairFees.setOrgSubID(context.getOrgSubId().intValue());
            bRepairFees.setTenantID(context.getTenantId().intValue());
        }
        bRepairFees.setCreateUserID(context.getUserId().intValue());
        Integer i = bRepairFeesMapper.insert(bRepairFees);
        return i;
    }

    @Override
    public int updateRepairFees(OperationContext context,BRepairFees bRepairFees) {
        Integer i = bRepairFeesMapper.updateById(bRepairFees);
        return i;
    }

    @Override
    public List deleteRepairFees(OperationContext context, OperateBase operateBase) {
        ArrayList<String> ids = operateBase.getIdList();
        List newId = new ArrayList<Integer>();
        BRepairFees bRepairFees  =  new BRepairFees();
        bRepairFees.setDelMark(Const.DEL_FLAG_DEL);
        for(String id:ids){
            bRepairFees.setId(Integer.valueOf(id));
            Integer i = bRepairFeesMapper.updateById(bRepairFees);
            if(i>0){
                newId.add(bRepairFees.getId());
            }
        }
        return newId;
    }

    public Map<String, Object>  isResultMap(List list, SearchCriteriaBase searchCriteriaBase){
        Map<String, Object>  map = new HashMap<>();
        map.put("count",list.size());
        map.put("limit",searchCriteriaBase.getLimit());
        map.put("pageIndex",searchCriteriaBase.getPageIndex());
        map.put("hasMore",false);
        map.put("items",list);
        return  map;
    }
}
